Skip to contentMethod: putIfAbsentAndGetNewKey(Object, Object)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Optional;
31: import java.util.concurrent.ConcurrentHashMap;
32:
33: /***********************************************************************************************************************
34: *
35: * A specialisation of {@link ConcurrentHashMap} with {@link Optional} support.
36: *
37: * @param <K> the type of the key
38: * @param <V> the type of the value
39: *
40: * @since 3.1-ALPHA-2
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44: public class ConcurrentHashMapWithOptionals<K, V> extends ConcurrentHashMap<K, V>
45: {
46: private static final long serialVersionUID = -1771492882183193296L;
47:
48: /*******************************************************************************************************************
49: *
50: * If the map doesn't contain the given key, put the new pair(key, value), and return the key itself. Otherwise,
51: * do nothing and return an empty {@link Optional}. The map manipulation is atomically performed by calling
52: * {@link #putIfAbsent(java.lang.Object, java.lang.Object)}.
53: *
54: * If {@code optionalKey} is empty, do nothing and return nothing.
55: *
56: * @param optionalKey the key
57: * @param value the value
58: * @return the new key, or nothing
59: *
60: ******************************************************************************************************************/
61: @Nonnull
62: public Optional<K> putIfAbsentAndGetNewKey (@Nonnull final Optional<K> optionalKey, @Nonnull final V value)
63: {
64: return optionalKey.flatMap(key -> putIfAbsentAndGetNewKey(key, value));
65: }
66:
67: /*******************************************************************************************************************
68: *
69: * If the map doesn't contain the given key, put the new pair(key, value), and return the key itself. Otherwise,
70: * do nothing and return an empty {@link Optional}. The map manipulation is atomically performed by calling
71: * {@link #putIfAbsent(java.lang.Object, java.lang.Object)}.
72: *
73: * @param key the key
74: * @param value the value
75: * @return the new key, or nothing
76: *
77: ******************************************************************************************************************/
78: @Nonnull
79: public Optional<K> putIfAbsentAndGetNewKey (@Nonnull final K key, @Nonnull final V value)
80: {
81:• return (putIfAbsent(key, value) == null) ? Optional.of(key) : Optional.empty();
82: }
83: }